extern alias JB;

namespace StyleCop.For.ReSharper.Core
{
    using System;
    using System.IO;
    using System.Security.Cryptography;

    using JetBrains.ProjectModel;

    public static class FileSystemOperations
    {
        /// <summary>
        /// Folder path where Shadow Folders / Files are kept.
        /// </summary>
        private const string ShadowCopyFolderPath = @"SCfR";

        public static void Method(IProjectFile projectFile)
        {
            //var realFilePath = projectFile.Location.FullPath.Remove(0, 3);

            //return Path.Combine(GetShadowFolderPath(), realFilePath);
        }

        public static string SHA1Hash(this string text)
        {
            try
            {
                SHA1 hash = SHA1.Create();

                byte[] writeBuffer;
                using (var ms = new MemoryStream())
                using (var sw = new StreamWriter(ms))
                {
                    sw.Write(text);
                    sw.Flush();
                    ms.Position = 0;
                    writeBuffer = hash.ComputeHash(ms);
                }
                string hashed = Convert.ToBase64String(writeBuffer);
                return hashed;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// IsGenerated the Shadow Copy file path.
        /// </summary>
        /// <param name="projectFile">
        /// File to create a shadow copy of.
        /// </param>
        /// <returns>
        /// Shadow Copy file path.
        /// </returns>
        public static FileInfo GetShadowFilePathForFile(IProjectFile projectFile)
        {
            var path = string.Empty;

            if (projectFile.ParentFolder != null)
            {
                var parentPathHashed = projectFile.ParentFolder.Name.SHA1Hash();
                var file = projectFile.Name;
                path = Path.Combine(parentPathHashed, file);
            }

            return new FileInfo(Path.Combine(ShadowFolderPath, path));
        }

        /// <summary>
        /// The path to the Shadow Folder Path.
        /// </summary>
        /// <value>
        ///   Shadow Folder Path.
        /// </value>
        public static string ShadowFolderPath
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ShadowCopyFolderPath);
            }
        }

        public static void CreateDirectory(string directoryName)
        {
            Directory.CreateDirectory(directoryName);
        }

        public static bool DirectoryExists(string directoryName)
        {
            return Directory.Exists(directoryName);
        }

        public static bool CreateFileAndWriteText(string path, string text)
        {
            bool success = false;

            if (!File.Exists(path))
            {
                try
                {
                    File.WriteAllText(path, text);
                    success = true;
                }
                catch (IOException exception)
                {
                    JB::JetBrains.Util.Logger.LogException(exception);
                }
            }
            else
            {
                File.WriteAllText(path, text);
                success = true;
            }

            return success;
        }

        public static void PurgeShadowFolder()
        {
            try
            {
                Directory.Delete(ShadowFolderPath, true);
            }
            catch (PathTooLongException exception)
            {
                JB::JetBrains.Util.Logger.LogException(exception);
            }
            catch (IOException exception)
            {
                JB::JetBrains.Util.Logger.LogException(exception);
            }
        }
    }
}